<?php
	class hotelRoomAvailableClass {
		protected $param = array(
								"apiKey" => null,
								"cid" => null,
								"locale" => null,
								"currencyCode" => null,
								"hotelId" => null,
								"arrivalDate" => null,
								"departureDate" => null,
								"occupancy" => null					
							);
							
		protected $wsdl = "http://api.ean.com/ean-services/ws/hotel/v3?wsdl&apiKey=t9grpd33pwudkegxpdrq6xp5";
		protected $objClient;
		protected $xmlParam;
		protected $soapvar;
		protected $objResponse;
		protected $response;
		protected $hotelRoomAvailable;
		protected $globalResult = array();
		
		protected function setXmlparam(){	
			$this->xmlParam = '<v3:getAvailability>
								 <HotelRoomAvailabilityRequest>
									<apiKey>t9grpd33pwudkegxpdrq6xp5</apiKey>
									<cid>55505</cid>
									<minorRev>9</minorRev>
									<customerIpAddress></customerIpAddress>
									<customerSessionId></customerSessionId>
									<customerUserAgent></customerUserAgent>
									<locale>en_US</locale>
									<currencyCode>USD</currencyCode>
									<hotelId>106347</hotelId>
									<arrivalDate>09/03/2013</arrivalDate>
									<departureDate>09/04/2013</departureDate> 
									<RoomGroup>
									   <Room>
										  <numberOfAdults>2</numberOfAdults>
										  <numberOfChildren>2</numberOfChildren>
										  <childAges>5</childAges>
										  <childAges>7</childAges>
									   </Room>
									</RoomGroup>
									<includeRoomImages>true</includeRoomImages>
								 </HotelRoomAvailabilityRequest>
							  </v3:getAvailability>';
		}
		
		protected function getdataParam(){
			$this->objClient = new SoapClient($this->wsdl, array('trace' => true,'exceptions' => 0,'features'=>SOAP_SINGLE_ELEMENT_ARRAYS, 'encoding' => 'UTF-8'));
			$this->setXmlparam();
			$this->soapvar = new SoapVar($this->xmlParam, XSD_ANYXML);
			$this->objResponse = $this->objClient->__soapCall("getAvailability", array($this->soapvar));
		}
	
		public function setSimpleXml(){
			$this->response = $this->objClient->__getLastResponse();
			$xml = simplexml_load_string($this->response, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
			$xml->registerXPathNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
			$xml->registerXPathNamespace('ns1', 'http://v3.hotel.wsapi.ean.com/');
			$result = $xml->xpath('//ns1:getAvailabilityResponse');
			$result = $result[0]->HotelRoomAvailabilityResponse;
			$this->hotelRoomAvailable = $result;
		}
		
		public function setParam($apiKey, $cid, $locale, $currencyCode,$hotelId, $arrivalDate, $departureDate, $occupancy){
			$this->param = array(
								"apiKey" => $apiKey,
								"cid" => $cid,
								"locale" => $locale,
								"currencyCode" => $currencyCode,
								"hotelId" => $hotelId,
								"arrivalDate" => null,
								"departureDate" => null,
								"occupancy" => null								
							);
					
			$this->getdataParam();
			$this->setSimpleXml();
		}
		
		public function getRoomAvailable(){
			$hotelRoom = $this->hotelRoomAvailable;
			
			$hotelRooms = array();
			foreach($hotelRoom->HotelRoomResponse as $hotelrm){
				
				$rooms = array();
				if(count($hotelrm->RateInfos->RateInfo->RoomGroup->Room) > 1){
					foreach($hotelrm->RateInfos->RateInfo->RoomGroup->Room as $rm){
						
						$childAges = array();
						if(isset($rm->childAges) && !empty($rm->childAges)){
							foreach($rm->childAges as $age){
								$childAges[] = (string) $age;
							}
						}
						
						$rooms[] = array(
									"numberOfAdults" => (string) $rm->numberOfAdults,
									"numberOfChildren" => (string) $rm->numberOfChildren,
									"childAges" => $childAges
						);
					}
				} else {
				
					$childAges = array();
					if(isset($hotelrm->RateInfos->RateInfo->RoomGroup->Room->childAges) && !empty($hotelrm->RateInfos->RateInfo->RoomGroup->Room->childAges)){
						foreach($hotelrm->RateInfos->RateInfo->RoomGroup->Room->childAges as $age){
							$childAges[] = (string) $age;
						}
					}
						
					$rooms[] = array(
									"numberOfAdults" => (string) $hotelrm->RateInfos->RateInfo->RoomGroup->Room->numberOfAdults,
									"numberOfChildren" => (string) $hotelrm->RateInfos->RateInfo->RoomGroup->Room->numberOfChildren,
									"childAges" => $childAges
						);
				}
				
				$hotelRooms[] = (object)array(
						"cancellationPolicy" => (string) $hotelrm->cancellationPolicy,
						"rateCode" => (string) $hotelrm->rateCode,
						"roomTypeCode" => (string) $hotelrm->roomTypeCode,
						"rateDescription" => (string) $hotelrm->rateDescription,
						"roomTypeDescription" => (string) $hotelrm->roomTypeDescription,
						"supplierType" => (string) $hotelrm->supplierType,
						"taxRate" => (string) $hotelrm->taxRate,
						"rateChange" => (string) $hotelrm->rateChange,
						"nonRefundable" => (string) $hotelrm->nonRefundable,
						"guaranteeRequired" => (string) $hotelrm->guaranteeRequired,
						"depositRequired" => (string) $hotelrm->depositRequired,
						"immediateChargeRequired" => (string) $hotelrm->immediateChargeRequired,
						"currentAllotment" => (string) $hotelrm->currentAllotment,
						"propertyId" => (string) $hotelrm->propertyId,
						"rateOccupancyPerRoom" => (string) $hotelrm->rateOccupancyPerRoom,
						"quotedOccupancy" => (string) $hotelrm->quotedOccupancy,
						"minGuestAge" => (string) $hotelrm->minGuestAge,
						"RateInfo" => (object) array( 
											"rateChange" => (string) $hotelrm->RateInfos->RateInfo['rateChange'], 
											"promo" => (string) $hotelrm->RateInfos->RateInfo['promo'],
											"priceBreakdown" => (string) $hotelrm->RateInfos->RateInfo['priceBreakdown'],
											"RoomGroup" => $rooms,
											"ChargeableRateInfo" => (object) array(
																"maxNightlyRate" => (string) $hotelrm->RateInfos->RateInfo->ChargeableRateInfo['maxNightlyRate'],
																"currencyCode" => (string) $hotelrm->RateInfos->RateInfo->ChargeableRateInfo['currencyCode'],
																"commissionableUsdTotal" => (string) $hotelrm->RateInfos->RateInfo->ChargeableRateInfo['commissionableUsdTotal'],
																'surchargeTotal' => (string) $hotelrm->RateInfos->RateInfo->ChargeableRateInfo['surchargeTotal'],
																'total' => (string) $hotelrm->RateInfos->RateInfo->ChargeableRateInfo['total']
											)
						),
						"deepLink" => (string) $hotelrm->deepLink
				);
			}
			
			$this->globalResult[] = (object)array(
								"customerSessionId" => (string) $hotelRoom->customerSessionId,
								"hotelId" => (string) $hotelRoom->hotelId,
								"arrivalDate" => (string) $hotelRoom->arrivalDate,
								"departureDate" => (string) $hotelRoom->departureDate,
								"hotelName" => (string) $hotelRoom->hotelName,
								"hotelAddress" => (string) $hotelRoom->hotelAddress,
								"hotelCity" => (string) $hotelRoom->hotelCity,
								"hotelStateProvince" => (string) $hotelRoom->hotelStateProvince,
								"hotelCountry" => (string) $hotelRoom->hotelCountry,
								"numberOfRoomsRequested" => (string) $hotelRoom->numberOfRoomsRequested,
								"checkInInstructions" => (string) $hotelRoom->checkInInstructions,
								"tripAdvisorRating" => (string) $hotelRoom->tripAdvisorRating,
								"hotelRooms" => $hotelRooms
			);
			
			return 	$this->globalResult;
		}
	}
	
	//$hotels = new hotelRoomAvailableClass();
	//$hotels->setParam("t9grpd33pwudkegxpdrq6xp5","55505","en_US","USD","127092","09/03/2013","09/04/2013","tes");
	//$hotels = $hotels->getRoomAvailable();
?>
<!--<pre><?php // print_r($hotels); ?></pre>-->